#include "widget.h"
#include "ui_widget.h"
#include <sys/stat.h>
#include <QDebug>
#include <QTime>
#include <QRegExp>
#include <QRegExpValidator>
extern QString testerAccount;//声明外部变量

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //将窗口移动到屏幕中央
    QDesktopWidget *desktop = QApplication::desktop();
    move( (desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
    //连接主线程的槽与子线程的信号
    m_thread = new workthread();
    connect(m_thread,SIGNAL(MsgSignal(const QString&)),this,SLOT(OnMsgSignal(const QString&)));
    connect(this,SIGNAL(sendData(QString)),m_thread,SLOT(receiveData(QString)));
    connect(this,SIGNAL(stopTest()),m_thread,SLOT(slot_stopTest()));
    ui->lb_account->setText(testerAccount);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPressEvent(QKeyEvent *ev)
{
    if(16777220 == ev->key() || 16777221 == ev->key())
    {
        ui->pushButton->clicked();
    }
}

void Widget::on_pushButton_clicked()
{
    QRegExp rx("YT\\dB\\d{8}");
    QRegExpValidator v(rx,0);
    int pos = 0;
    QValidator::State res;
    QString text = ui->lineEdit->text();
    res = v.validate(text,pos);
    qDebug()<< "------------" <<res ;
    qDebug()<< "-******" <<QValidator::Acceptable ;
    if(QValidator::Acceptable == res)
    {
        m_thread->start();
        emit sendData(ui->lineEdit->text());
        ui->textBrowser->clear();
        ui->lb_sn->setText(text);
        //ui->lineEdit->clear();
    }
    else
    {
        ui->textBrowser->clear();
        ui->textBrowser->setText("整机编号录入有误，请核对！！！");
    }
}

void Widget::OnMsgSignal(const QString &temp)
{
    ui->textBrowser->append(QTime::currentTime().toString() + "     " + temp);
}

void Widget::on_pushButton_2_clicked()
{
    emit stopTest();
//    m_thread->terminate();
}
